﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using GmaWebService.Interfaces;
using System.IO;
namespace TV4Home.MvcWebInterface.UI.WebService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "GmaWebService" in code, svc and config file together.
    public class GmaWebService : IMediaAccessService,IStreamingService
    {
        #region Fields
        private static IMediaAccessService _service;
        private static IStreamingService _service2;
        #endregion

        #region Properties
        public static IMediaAccessService ServiceInstance
        {
            get
            {
                if (_service == null || ((ICommunicationObject)_service).State == CommunicationState.Faulted)
                {
                    _service = ChannelFactory<IMediaAccessService>.CreateChannel(new NetNamedPipeBinding() { MaxReceivedMessageSize = 10000000 }, new EndpointAddress("net.pipe://localhost/GmaWebService/MediaAccessService"));
                }

                return _service;
            }
        }
        public static IStreamingService ServiceInstance2
        {
            get
            {
                if (_service == null || ((ICommunicationObject)_service).State == CommunicationState.Faulted)
                {
                    _service2 = ChannelFactory<IStreamingService>.CreateChannel(new NetNamedPipeBinding() { MaxReceivedMessageSize = 10000000 }, new EndpointAddress("net.pipe://localhost/GmaWebService/MediaAccessService/stream"));
                }

                return _service2;
            }
        }
        #endregion

         public WebSupportedMPFunctions MP_GetSupportedFunctions()
        {
                      return ServiceInstance.MP_GetSupportedFunctions();
        }

        #region Music
        public List<WebShare> MP_GetAllMusicShares()
        {
            return ServiceInstance.MP_GetAllMusicShares();
        }

        public WebMusicTrack MP_GetMusicTrack(int trackId)
        {
            return ServiceInstance.MP_GetMusicTrack(trackId);
        }

        public int MP_GetMusicTracksCount()
        {
            return ServiceInstance.MP_GetMusicTracksCount();
        }

        public List<WebMusicTrack> MP_GetAllMusicTracks()
        {
            return ServiceInstance.MP_GetAllMusicTracks();
        }

        public List<WebMusicTrack> MP_GetMusicTracks(int startIndex, int endIndex)
        {
            return ServiceInstance.MP_GetMusicTracks(startIndex, endIndex);
        }



        public List<WebMusicAlbum> MP_GetAllAlbums()
        {
            return ServiceInstance.MP_GetAllAlbums();
        }


        public List<WebMusicAlbum> MP_GetAlbums(int startIndex, int endIndex)
        {
            return ServiceInstance.MP_GetAlbums(startIndex, endIndex);
        }
        public WebMusicAlbum MP_GetAlbum(string artist, string album)
        {
            return ServiceInstance.MP_GetAlbum(artist, album);
        }


        public int MP_GetAlbumsCount()
        {
            return ServiceInstance.MP_GetAlbumsCount();
        }


        public List<WebMusicArtist> MP_GetAllArtists()
        {
            return ServiceInstance.MP_GetAllArtists();
        }

        public List<WebMusicArtist> MP_GetArtists(int startIndex, int endIndex)
        {
            return ServiceInstance.MP_GetArtists(startIndex, endIndex);
        }
   
        public int MP_GetArtistsCount()
        {
            return ServiceInstance.MP_GetArtistsCount();
        }


        public List<WebMusicAlbum> MP_GetAlbumsByArtist(String artistName)
        {
            return ServiceInstance.MP_GetAlbumsByArtist(artistName);
        }


        public List<WebMusicTrack> MP_GetSongsOfAlbum(String albumName, String albumArtistName)
        {
            return ServiceInstance.MP_GetSongsOfAlbum(albumName, albumArtistName);
        }


        public List<WebMusicTrack> MP_FindMusicTracks(string album, string artist, string title)
        {
            return ServiceInstance.MP_FindMusicTracks(album, artist, title);
        }
        #endregion
        #region videos
        public List<WebShare> MP_GetVideoShares()
        {
            return ServiceInstance.MP_GetVideoShares();
        }

        public int MP_GetVideosCount()
        {
            return ServiceInstance.MP_GetVideosCount();
        }


        public List<WebMovie> MP_GetAllVideos()
        {
            return ServiceInstance.MP_GetAllVideos();//get all movies from beginning to end
        }


        public List<WebMovie> MP_GetVideos(int startIndex, int endIndex)
        {
            return ServiceInstance.MP_GetVideos(startIndex, endIndex);
        }

        public WebMovieFull MP_GetFullVideo(int videoId)
        {
            return ServiceInstance.MP_GetFullVideo(videoId);
        }

        #endregion
        #region TvSeries

        public int MP_GetSeriesCount()
        {
            return ServiceInstance.MP_GetSeriesCount();
        }


        public List<WebSeries> MP_GetAllSeries()
        {
            return ServiceInstance.MP_GetAllSeries();
        }


        public List<WebSeries> MP_GetSeries(int startIndex, int endIndex)
        {
            return ServiceInstance.MP_GetSeries(startIndex, endIndex);
        }


        public WebSeriesFull MP_GetFullSeries(int seriesId)
        {
            return ServiceInstance.MP_GetFullSeries(seriesId);
        }


        public List<WebSeason> MP_GetAllSeasons(int seriesId)
        {
            return ServiceInstance.MP_GetAllSeasons(seriesId);//all seasons;
        }


        public WebSeason MP_GetSeason(int seriesId, int seasonNumber)
        {
            return ServiceInstance.MP_GetSeason(seriesId, seasonNumber);
        }


        public List<WebEpisode> MP_GetAllEpisodes(int seriesId)
        {
            return ServiceInstance.MP_GetAllEpisodes(seriesId);
        }


        public List<WebEpisode> MP_GetAllEpisodesForSeason(int seriesId, int seasonNumber)
        {
            return ServiceInstance.MP_GetAllEpisodesForSeason(seriesId, seasonNumber);
        }


        public List<WebEpisode> MP_GetEpisodes(int seriesId, int startIndex, int endIndex)
        {
            return ServiceInstance.MP_GetEpisodes(seriesId, startIndex, endIndex);
        }


        public List<WebEpisode> MP_GetEpisodesForSeason(int seriesId, int seasonId, int startIndex, int endIndex)
        {
            return ServiceInstance.MP_GetEpisodesForSeason(seriesId, seasonId, startIndex, endIndex);
        }


        public int MP_GetEpisodesCount(int seriesId)
        {
            return ServiceInstance.MP_GetEpisodesCount(seriesId);
        }


        public int MP_GetEpisodesCountForSeason(int seriesId, int season)
        {
            return ServiceInstance.MP_GetEpisodesCountForSeason(seriesId, season);
        }


        public WebEpisodeFull MP_GetFullEpisode(int episodeId)
        {
            return ServiceInstance.MP_GetFullEpisode(episodeId);
        }

        #endregion
        #region Movies

        public WebMovieFull MP_GetFullMovie(int movieId)
        {
            return ServiceInstance.MP_GetFullMovie(movieId);
        }


        public int MP_GetMovieCount()
        {
            return ServiceInstance.MP_GetMovieCount();
        }


        public List<WebMovie> MP_GetAllMovies()
        {
            return ServiceInstance.MP_GetAllMovies();//get all movies from beginning to end
        }


        public List<WebMovie> MP_GetMovies(int startIndex, int endIndex)
        {
            return ServiceInstance.MP_GetMovies(startIndex, endIndex);
        }


        public List<WebMovie> MP_SearchForMovie(String searchString)
        {
            return ServiceInstance.MP_SearchForMovie(searchString);
        }
                #endregion




        #region FileSystemAccess
        public List<String> FS_GetDirectoryListByPath(string path)
        {
           

            return ServiceInstance.FS_GetDirectoryListByPath(path);
        }

        public List<WebFileInfo> FS_GetFilesFromDirectory(string path)
        {
            
            return ServiceInstance.FS_GetFilesFromDirectory(path);
        }

        public WebFileInfo FS_GetFileInfo(string path)
        {

            return ServiceInstance.FS_GetFileInfo( path);
        }


        public byte[] FS_GetFile(string filePath)
        {
        return ServiceInstance2.FS_GetFile(filePath);
        }
        #endregion

        #region Pictures
        public List<String> MP_GetPictureRootShares(string path)
        {
            throw new NotImplementedException();
       
        }
        public WebPictureDirectory MP_GetPictureDirectory(string path)
        {
 return ServiceInstance.MP_GetPictureDirectory(path);
        }
        
    

        #endregion

        #region Images

        /// <summary>
        /// Returns the image object of the given path or null if the image doesn't exists
        /// </summary>
        /// <param name="path">Path to image</param>
        /// <returns>Stream of image or null</returns>
        public Stream FS_GetImage(string path)
        {
            return ServiceInstance2.FS_GetImage(path);
        }




        /// <summary>
        /// Returns the image object of the given path, resized to fit the maxWidth and maxHeight
        /// parameters, or null if the image doesn't exists
        /// </summary>
        /// <param name="path">Path to image</param>
        /// <param name="maxWidth">Maximum width of image</param>
        /// <param name="maxHeight">Maximum height of image</param>
        /// <returns>Stream of image or null</returns>
        public Stream FS_GetImageResized(string path, int maxWidth, int maxHeight)
        {

            return ServiceInstance2.FS_GetImageResized(path, maxWidth, maxHeight);
           
        }

        /// <summary>
        /// Returns the stream to a media item (video, music, image,...)
        /// </summary>
        /// <param name="path">Path to the local file</param>
        /// <returns>Stream of file or null if file not found</returns>
        public Stream FS_GetMediaItem(string path)
        {
            return ServiceInstance2.FS_GetMediaItem(path);
        }
        

        #endregion
    }
}
